<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:touch-action="http://www.w3.org/1999/xhtml">
<head>
    <link rel="shortcut icon" href="/favicon.ico"/>
    <link rel="bookmark" href="/favicon.ico"/>
    <meta charset="UTF-8"/>
    <title>用户登录</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/admin.css}" href="../../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" th:href="@{/layuiadmin/style/login.css}" href="../../layuiadmin/style/login.css" media="all">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script th:src="@{/js/jquery.cookie.js}"></script>
</head>
<body>
<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>用户登录</h2>
            <p>欢迎来到行影旅行社</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
                <input type="text" name="login" id="LAY-user-login-username" lay-verify="required" placeholder="用户名/手机号/邮箱" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
                        <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required" placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
                <a id="forget"  class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button id="login"  class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
            </div>
            <div class="layui-trans layui-form-item layadmin-user-login-other">
                <label>社交账号登入</label>
                <a href="javascript:;"><i class="layui-icon layui-icon-login-qq"></i></a>
                <a href="/dist/weCatLogin"><i class="layui-icon layui-icon-login-wechat"></i></a>
                <a href="javascript:;"><i class="layui-icon layui-icon-login-weibo"></i></a>

                <a href="register" class="layadmin-user-jump-change layadmin-link">注册帐号</a>
            </div>
        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2019
            <a href="/dist/view" target="_blank">xingying.com</a>
        </p>
    </div>

</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function() {
        var $ = layui.$,
            setter = layui.setter,
            admin = layui.admin,
            form = layui.form,
            router = layui.router(),
            search = router.search;
        form.render();
        $(function () {
            window.onload = getCookie()
        })

        /*验证码的key*/
        var verKey;

        $(function(){
            // 获取验证码
            $.get('/captcha/getArithmeticCaptcha', function(res) {
                if (res.code == 200) {
                    verKey = res.data.key;
                    $('#LAY-user-get-vercode').attr('src', res.data.value);
                }
            },'json');
        })
        //提交
       /* form.on('submit(LAY-user-login-submit)', function(obj) {

            $.ajax({
                type: "POST",
                url: "/user/login",
                data: JSON.stringify(obj.field),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function(result) {
                    if(result.code == 0) {
                        //登录成功，写入 access_token
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName,
                            value: result.sessionId
                        });
                        //登入成功的提示与跳转
                        layer.msg('登入成功', {
                            offset: '15px',
                            icon: 1,
                            time: 1000
                        }, function() {
                            location.href = 'view'; //后台主页
                           // location.href="javascript:history.go(-1)"
                        });
                    } else {
                        layer.msg(result.message, {
                            icon: 5
                        });
                    }
                }
            });
        });
    });
*/
        form.on('submit(LAY-user-login-submit)', function(obj) {
            //判断是否选中复选框，如果选中，添加cookie
            var rememberPwd = $("input[type='checkbox']").is(':checked');
            // console.log("是否记住密码：" + rememberPwd);
            if (rememberPwd) {
                //添加cookie
                setCookie();
            } else {
                clearAllCookie();
            }
            $.ajax({
                url: "/user/login?verKey="+verKey,
                type: "post",//请求类型
                data: JSON.stringify(obj.field),
                contentType:  "application/json; charset=utf-8",
                dataType: "json",//服务端返回给浏览器的数据类型
                success: function (result) {//执行成功的回调函数,次data非彼data，是服务器端返回的json的一个变量
                    if (result.code == 200) {
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName,
                            value: result.sessionId
                        });
                        layer.msg('登入成功', {
                            offset: '15px',
                            icon: 1,
                            time: 1000
                        }, function() {
                            location.href = 'index'; //后台主页
                            // location.href="javascript:history.go(-1)"
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 5
                        });
                    }
                }
            });
        });

/*
        function a() {
            alert(2135)
            var name = $("#LAY-user-login-username").val();
            var password = $("#LAY-user-login-password").val();
            var vercode = $("#LAY-user-login-vercode").val();
            var parma = {
                "login" : name,
                "password" : password,
                "vercode" : vercode
            }
            $.ajax({
                url: "/user/login",
                type: "post",//请求类型
                data: JSON.stringify(parma),
                contentType:  "application/json; charset=utf-8",
                dataType: "json",//服务端返回给浏览器的数据类型
                success: function (result) {//执行成功的回调函数,次data非彼data，是服务器端返回的json的一个变量
                    if (result.code == 200) {
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName,
                            value: result.sessionId
                        });
                        layer.msg('登入成功', {
                            offset: '15px',
                            icon: 1,
                            time: 1000
                        }, function() {
                            location.href = 'index'; //后台主页
                            // location.href="javascript:history.go(-1)"
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 5
                        });
                    }
                }
            });
        }
*/

        /*
       * 记住密码：设置cookic*/
        //设置cookie
        function setCookie() {
            var loginName = $("#LAY-user-login-username").val();
            var password = $("#LAY-user-login-password").val();
            var checked = $("input[type='checkbox']").is(':checked'); //获取“是否记住密码”复选框
            //console.log("setCookie方法是否记住密码："+checked);
            if (checked) { //判断是否选中了“记住密码”复选框
                //设置cookie过期时间
                // var date = new Date();
                // date.setTime(date.getTime() + 60 * 1000); //只能这么写，60表示60秒钟
                //console.log("cookie过期时间："+date);
                $.cookie("loginName", loginName, {
                    expires: 7,
                    path: '/'
                }); //调用jquery.cookie.js中的方法设置cookie中的用户名
                $.cookie("password", password, {
                    expires: 7,
                    path: '/'
                }); //调用jquery.cookie.js中的方法设置cookie中的登陆密码，并使用base64（jquery.base64.js）进行加密
            } else {
                $.cookie("loginName", null);
                $.cookie("password", null);
            }
        }

        //获取cookie
        function getCookie() {
            var loginName = $.cookie("loginName"); //获取cookie中的用户名
            var password = $.cookie("password"); //获取cookie中的登陆密码
            // console.log("获取cookie:账户：" + loginCode);
            // console.log("获取cookie:密码：" + pwd);
            if (!loginName || loginName == 0) {
                // console.log("账户：" + !loginCode);
                // alert("请输出内容！");
            } else {
                $("#LAY-user-login-username").val(loginName);
            }
            if (!password || password == 0) {
                // console.log("密码：" + !pwd);
            } else {
                //密码存在的话把密码填充到密码文本框
                //console.log("密码解密后："+$.base64.decode(pwd));
                $("#LAY-user-login-password").val(password);
                //密码存在的话把“记住用户名和密码”复选框勾选住
                $("[name='remember']").attr("checked", "true");
            }
            form.render();

        }

        //清除所有cookie函数
        function clearAllCookie() {
            var date = new Date();
            date.setTime(date.getTime() - 10000);
            var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
            // console.log("需要删除的cookie名字：" + keys);
            if (keys) {
                for (var i = keys.length; i--;)
                    document.cookie = keys[i] + "=0; expire=" + date.toGMTString() + "; path=/";
            }
        }
        //刷新验证码
        $("#LAY-user-get-vercode").click(function () {
            // 获取验证码
            $.get('/captcha/getArithmeticCaptcha', function(res) {
                if (res.code == 200) {
                    verKey = res.data.key;
                    $('#LAY-user-get-vercode').attr('src', res.data.value);
                }
            },'json');
        })

    });



    //按回车键实现登录
    /*$(document).keydown(function (event) {
        if (event.keyCode == 13) {

            $("#LAY-user-login-submit").trigger("click");
        }
    })*/


    $(document).on('keydown', function (event) {
        if (event.keyCode == 13) {
            $("#login").trigger("click")
            return false;
        }})

    /*$("#forget").click(function () {
        alert(1)
    })*/

    $("#forget").click(function () {
        var email = $("#LAY-user-login-username").val();
        $.post("/validate/sendValidationEmail?email="+email,function(data) { //get类型只要修改$.post为$.get
            if (data.code==200){
                alert(data.data.token)
                $.cookie("restToken", data.data.token);
                swal("密码重置链接已发送至QQ邮箱中...", "请注意查收!", "success");
            }else{
                layer.msg(data.msg, {
                    offset: '15px',
                    icon: 1,
                    time: 1000
                })
            }
        },"json");
    })


</script>

</body>
</html>